using System;
using System.Collections.Generic;
using System.Linq;

namespace ALApps.Graphics.Interface.Items
{
    public enum Allignment
    {
        Vertical,
        Horizontal
    }

    public class ListBox : TextureBlock, IDrawableItem
    {
        #region fields
        private Allignment allignment;
        private int itemIndex;
        #endregion

        #region properties
        /// <summary>
        /// The first index to be drawn.
        /// </summary>
        public int ItemIndex
        { 
            get { return itemIndex; }
            set
            {
                indexChanged(itemIndex, value);
                itemIndex = value;
            }
        }

        /// <summary>
        /// The width of the items.
        /// </summary>
        public int ItemWidth { get; set; }

        /// <summary>
        /// The height of the items.
        /// </summary>
        public int ItemHeight { get; set; }

        /// <summary>
        /// The number of items that should be accounted for when calculating the size.
        /// </summary>
        public int ItemCount { get; set; }

        public string Allignment 
        {
            get { return allignment.ToString(); }
            set { allignment = SetAllignment(value); }
        }
        #endregion

        #region initialization
        public override void Initialize()
        {
            if (allignment == Items.Allignment.Horizontal)
            {
                Width = ItemCount * ItemWidth;
                Height = ItemHeight;
            }
            else
            {
                Width = ItemWidth;
                Height = ItemCount * ItemHeight;
            }

            base.Initialize();
        }
        #endregion

        #region methods
        private void indexChanged(int index, int oldIndex)
        {
            int delta = index - oldIndex;

            foreach (var item in Childs.Values)
            {
                item.Visble = false;
                item.Focusabel = false;
                if (allignment == Items.Allignment.Horizontal)
                {
                    item.X += delta * ItemWidth;
                }
                else
                {
                    item.Y += delta * ItemHeight;
                }
            }

            if (allignment == Items.Allignment.Horizontal)
            {
                var items = (from i in Childs.Values where i.X >= 0 && i.X + i.Width <= Width select i);
                foreach (var item in items)
                {
                    item.Visble = true;
                    item.Focusabel = true;
                }
            }
            else
            {
                var items = (from i in Childs.Values where i.Y >= 0 && i.Y + i.Height <= Height select i);
                foreach (var item in items)
                {
                    item.Visble = true;
                    item.Focusabel = true;
                }
            }
        }

        private Items.Allignment SetAllignment(string allignment)
        {
           return (Items.Allignment)Enum.Parse(typeof(Items.Allignment), allignment);
        }

        public void SetItems(List<dynamic> objects, Func<dynamic, Item> generator)
        {
            Childs.Clear();

            List<Item> items = new List<Item>();

            foreach (var o in objects)
                items.Add(generator(o));


            if (allignment == Items.Allignment.Vertical)
            {
                int h = 0;
                foreach (var item in items)
                {
                    item.Visble = false;
                    item.Focusabel = false;
                    item.Width = ItemWidth;
                    item.Height = ItemHeight;
                    item.X = 0;
                    item.Y = h;
                    h += item.Height;
                }
            }
            else
            {
                int w = 0;
                foreach (var item in items)
                {
                    item.Visble = false;
                    item.Focusabel = false;
                    item.Width = ItemWidth;
                    item.Height = ItemHeight;
                    item.Y = 0;
                    item.X = w;
                    w += item.Width;
                }
            }

            if (allignment == Items.Allignment.Horizontal)
            {
                foreach (var item in (from i in items where i.X >= 0 && i.X + i.Width <= Width select i))
                {
                    item.Visble = true;
                    item.Focusabel = true;
                }
            }
            else
            {
                foreach (var item in (from i in items where i.Y >= 0 && i.Y + i.Height <= Height select i))
                {
                    item.Visble = true;
                    item.Focusabel = true;
                }
            }

            AddChilds(items);

        }
        #endregion
    }
}
